range
A range of integer values. Ranges represent arithmetic sequences with defined start and end points, and a constant difference between consecutive elements. Ranges can be empty or contain any natural number of elements.
Range is a subtype of iterable<integer>
.
Since
0.6.0
See also
for inherited values and methods
Constructors
Construct a range, starting at 0
(inclusive), ending at end
(exclusive), with a step size of 1
.
Examples:
list(range(0))
returns[]
.list(range(1))
returns[0]
.list(range(2))
returns[0, 1]
.list(range(10))
returns[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
.
Note that range(x)
is equivalent to range(0, x, 1)
.
Construct a range, starting at start
(inclusive), ending at end
(exclusive), with a "step size" (i.e. difference between consecutive values) of step
.
If start > end
, then step
must be negative. Conversely, if start < end
, the step
must be positive. step
cannot be 0
.
Examples:
list(range(1, 23, 3))
returns[1, 4, 7, 10, 13, 16, 19, 22]
.list(range(20, 1, -2))
returns[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
.list(range(-2, -8, -1))
returns[-2, -3, -4, -5, -6, -7]
.list(range(3, 3, 7))
returns[]
. Indeed, for allx
, and for ally != 0
,range(x, x, y)
returns[]
.
Note that range(0, x, 1)
is equivalent to range(x)
.
Functions
Generate a textual representation of this iterable.
An optional separator, prefix and postfix can be provided. One can also provide a limit: integer?
. If there are more elements in the result than limit
, the elements whose indices exceed limit
are omitted, and the passed truncated: text
is included instead.
Examples:
[1, 2, 3].join_to_text()
returns'1, 2, 3'
.[1, 2, 3].join_to_text('_')
returns'1_2_3'
.[1, 2, 3].join_to_text('*', '(', ')')
returns'(1*2*3)'
.list<T>().join_to_text('!', '(', ')')
returns'()'
(whereT
is a valid type).range(10).join_to_text('', '', '', 5)
returns'01234...'
.range(10).join_to_text('', '', '', 5, 'more')
returns'01234more'
.
Where the function even
is defined:
function even(x: integer): text {
return if (x % 2 == 0) 'EVEN' else 'ODD';
}
Then:
range(10).join_to_text('->', '{', '}', 5, '...', even(*))
returns{EVEN->ODD->EVEN->ODD->EVEN->...}
.